#include "Tickettable.h"

Tickettable::Tickettable(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	this->setWindowTitle("地铁票价表");
    this->setWindowIcon(QIcon(":Icon/image/BeijingSubwaylogo.png"));
    ui.tabWidget->setCurrentIndex(0); //设置默认tab页

    /*获取文件行数（包含表头）*/
    QFile temp1(":/files/files/ticketPrice.txt");
    temp1.open(QIODevice::ReadOnly);
    QTextStream in(&temp1);
    int rowNum = 0; //文件行数——包含表头
    int colNum = 0;//文件列数
    while (true) {
        QString temp = in.readLine();
        if (temp.isNull()) {
            break;
        }
        if (rowNum == 0) {  //第零行，根据表头获取列数
            QStringList part = temp.split(",");
            colNum = part.size();
        }
        rowNum++;
    }
    temp1.close();

    QFile temp2(":/files/files/sale.txt");
    temp2.open(QIODevice::ReadOnly);
    QTextStream in1(&temp2);
    int rowNum2 = 0; //文件行数——包含表头
    int colNum2 = 0;//文件列数
    while (true) {
        QString temp = in1.readLine();
        if (temp.isNull()) {
            break;
        }
        if (rowNum2 == 0) {  //第零行，根据表头获取列数
            QStringList part = temp.split(",");
            colNum2 = part.size();
        }
        rowNum2++;
    }
    temp2.close();


    ui.tableWidget->setRowCount(rowNum-1); //不计表头
    ui.tableWidget->setColumnCount(colNum);
    ui.tableWidget_2->setRowCount(rowNum2 - 1); //不计表头
    ui.tableWidget_2->setColumnCount(colNum2);
    
    /*设置字体和表头居中*/
    ui.tableWidget->setFont(QFont("Times New Roman", 12));
    ui.tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
    ui.tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background-color:rgb(40,143,218);font:13pt '宋体';color: white;};");
    ui.tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);//单元格不可编辑

    ui.tableWidget_2->setFont(QFont("Times New Roman", 12));
    ui.tableWidget_2->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
    ui.tableWidget_2->horizontalHeader()->setStyleSheet("QHeaderView::section{background-color:rgb(40,143,218);font:13pt '宋体';color: white;};");
    ui.tableWidget_2->setEditTriggers(QAbstractItemView::NoEditTriggers);//单元格不可编辑

    /*将每行读入tableWidget——普通票*/
    QFile price(":/files/files/ticketPrice.txt");
    price.open(QIODevice::ReadOnly);
    QTextStream in2(&price);
    for (int i = 0; i < rowNum ; i++) {
        QString infoP = in2.readLine();
        infoP = infoP.trimmed();
        if (infoP.isNull()) {
            break;
        }
        QStringList parts = infoP.split(",");
        ui.tableWidget->resize(400, 300);  //设置表格大小
        if (i == 0) {      //设置表头
            ui.tableWidget->setHorizontalHeaderLabels(parts);
        }
        else {
            for (int j = 0; j < parts.size(); j++) {
                if (parts.at(0).at(0).toLatin1() != '/') {
                    ui.tableWidget->setItem(i-1 , j, new QTableWidgetItem(parts.at(j)));  //虽然i=1，但是是内容的第零行
                    ui.tableWidget->item(i - 1, j)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
                }
                else {
                    QString perKM(parts.at(0));
                    QString add(parts.at(1));
                    perKM.remove("/");
                    add.remove("+");
                    ui.tableWidget->setSpan(i-1 , 0, 1, colNum);  //当前行合并为一个单元格
                    QString end;
                    end = "此后每增加" + perKM + "公里，增收" + add + "元";
                    ui.tableWidget->setItem(i-1 , 0, new QTableWidgetItem(end));
                    ui.tableWidget->item(i - 1, 0)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
                }
            }
        }
        
    }
    /*将每行读入tableWidget——旅游票*/
    QFile sale(":/files/files/sale.txt");
    sale.open(QIODevice::ReadOnly);
    QTextStream in3(&sale);
    for (int i = 0; i < rowNum2; i++) {
        QString infoP = in3.readLine();
        infoP = infoP.trimmed();
        if (infoP.isNull()) {
            break;
        }
        QStringList parts = infoP.split(",");
        ui.tableWidget_2->resize(400, 300);  //设置表格大小
        if (i == 0) {      //设置表头
            ui.tableWidget_2->setHorizontalHeaderLabels(parts);
        }
        else {
            for (int j = 0; j < parts.size(); j++) {
                if (parts.at(0).at(0).toLatin1() != '>') {
                    ui.tableWidget_2->setItem(i - 1, j, new QTableWidgetItem(parts.at(j)));  //虽然i=1，但是是内容的第零行
                    ui.tableWidget_2->item(i - 1, j)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
                }
                else {
                    QString upLimit(parts.at(0));
                    QString cheap(parts.at(1));
                    upLimit.remove(">");
                    QString end1, end2;
                    end1 = upLimit + "个以上";
                    end2 = cheap;
                    ui.tableWidget_2->setItem(i - 1, 0, new QTableWidgetItem(end1));
                    ui.tableWidget_2->setItem(i - 1, 1, new QTableWidgetItem(end2));
                    ui.tableWidget_2->item(i - 1, 0)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
                    ui.tableWidget_2->item(i - 1, 1)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
                }
            }
        }
    }
    ui.tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);    //x先自适应宽度
    ui.tableWidget_2->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);    //x先自适应宽度
    ui.tableWidget->show();
    ui.tableWidget_2->show();
    price.close();
    sale.close();

}

Tickettable::~Tickettable()
{}
